﻿using huqiang.Framework;
using huqiang.Framework.Http;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DownloadStep : AssetsAction
{
    int maxCount = 0;
    long maxLength;
    int downloadCount = 0;
    long downloadBytes;
    int failCount;
    public override void Reset()
    {
        base.Reset();
    }
    public override void Update()
    {
        if (downloadCount >= maxCount)
            _done = true;
        else _done = false;
    }
    public void DownLoadAssets(List<AssetsInfo> assets, string block, string url, bool useBlock = true)
    {
        string df = "";
        if (useBlock)
        {
            df = "/" + block;
        }
        HttpManager manager = HFBase.GetModule<HttpManager>();
        string folder = LocalSetting.AssetsPath+df;
        for (int j = 0; j < assets.Count; j++)
        {
            var kv = assets[j];
            string name = assets[j].name;
            manager.Download(folder, name, url + df + "/" + name, block, assets[j].value,
                DownloadCompelete, assets[j].len);
            maxCount++;
            maxLength += assets[j].len;
        }
    }
    public  void DownloadCompelete(IMission mission)
    {
        var http = mission as HttpDownloader;
        if (http.code == 200)
        {
            downloadCount++;
            downloadBytes += http.Length;
        }
        else
        {
            failCount++;
            mission.Reset();
            ///下载失败，放置任务尾部重新下载
            //HttpFileManager.AddMission(http);
            HFBase.GetModule<HttpManager>().AddMission(http);
            AssetsVersionControl.SaveRecord();
        }
    }
    void DownloadOtherCompelete(IMission mission)
    {
        var http = mission as HttpDownloader;
        if (http.code == 200)
        {
            downloadCount++;
            downloadBytes += http.Length;
        }
        else
        {
            failCount++;
            mission.Reset();
            ///下载失败，放置任务尾部重新下载
            //HttpFileManager.AddMission(http);
            HFBase.GetModule<HttpManager>().AddMission(http);
            AssetsVersionControl.SaveRecord();
        }
    }
    public override string Tips { get {

            double d = downloadBytes + HFBase.GetModule<HttpManager>().GetDownloadBytes();
            d /= 1048576;
            double a = maxLength;
            a /= 1048576;
            double r = 0;
            if (a != 0)
                r = d / a;
            d = System.Math.Round(d, 2);
            a = System.Math.Round(a, 2);
            int p = (int)(r * 100);
            int da = downloadCount;
            if (da < maxCount)
                da++;

            string str = "正在下载资源包:" + da.ToString() + "/" + maxCount;
            str = str + "   " + d.ToString();
            str = str + "/" + a.ToString() + "MB";
            str = str + "   " + p + "%";
            return str;
        }
    }
    public override float Progress { get {
            if (maxLength == 0)
                return 0;
            double d = downloadBytes + HFBase.GetModule<HttpManager>().GetDownloadBytes();
            double a = maxLength;
            return (float)(d / a);
        } }
}